using UnityEngine;
using TinaX;
using TinaX.Services;
using Nekonya.Managers;
using UnityEngine.SceneManagement;
using Nekonya.Dicts;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Nekonya
{
    [DisallowMultipleComponent]
    public class GameStartup : MonoBehaviour
    {
        async void Start()
        {
            IXCore core = XCore.New()
                .UseUIKit()
                .UseVFS()
                .UseI18N()
                .ConfigureServices(services =>
                {
                    services.Singleton<IGlobalManager, GlobalManager>();
                    services.Singleton<IGameModeManager, GameModeManager>();
                    services.Singleton<BagManager>();
                    services.Singleton<DictFactory>();
                })
                .OnServicesStartException((serviceName, error) =>
                {
                    Debug.LogErrorFormat("框架启动阶段，服务\"{0}\"发生错误：{1}", serviceName, error.Message);
                });

            await core.RunAsync();
        }


#if UNITY_EDITOR
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        public static void OnGameStart()
        {
            var cur_scene = SceneManager.GetActiveScene();
            if (!cur_scene.name.Equals("Game.Startup") && cur_scene.name.StartsWith("Game."))
            {
                SceneManager.LoadScene("Game.Startup");
            }
        }
#endif
    }



#if UNITY_EDITOR

    [CustomEditor(typeof(GameStartup))]
    public class GameStartupCustom : Editor
    {
        private GUIStyle _titleLabelStyle; 
        private GUIStyle m_TitleLabelStyle
        {
            get
            {
                if(_titleLabelStyle == null)
                {
                    _titleLabelStyle = new GUIStyle(EditorStyles.largeLabel);
                    _titleLabelStyle.alignment = TextAnchor.MiddleCenter;
                    _titleLabelStyle.fontStyle = FontStyle.Bold;
                    _titleLabelStyle.fontSize = 16;
                }
                return _titleLabelStyle;
            }
        } 

        public override void OnInspectorGUI()
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("这里是启动游戏的入口", m_TitleLabelStyle);
            EditorGUILayout.Space();

            base.OnInspectorGUI();
        }
    }
#endif

}



